<template>
  <div class="app-container" v-if="showDetail">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="真实姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="card">
        <el-input
          v-model="queryParams.card"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['renter:hirer:add']"
        >新增</el-button>
      </el-col>

      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['renter:hirer:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->

      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['renter:hirer:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->

      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['renter:hirer:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hirerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="公司名" align="center" prop="company" />
      <el-table-column label="业主姓名" align="center" prop="name" />
      <el-table-column label="身份证号" align="center" prop="card" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_hirer_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="租赁的商铺个数" align="center" prop="rentCount" />
      <el-table-column label="拥有的商铺个数" align="center" prop="haveCount" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['renter:hirer:edit']"
          >修改</el-button>

<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            @click="handleDetail(scope.row)"-->
<!--            v-hasPermi="['renter:hirer:edit']"-->
<!--          >明细</el-button>-->

          <el-button size="mini" type="text" @click="handleDetail(scope.row)">明细</el-button>


        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司名" maxlength="50" show-word-limit/>
        </el-form-item>
        <el-form-item label="业主姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入业主姓名" maxlength="30" show-word-limit/>
        </el-form-item>
        <el-form-item label="身份证号" prop="card">
          <el-input v-model="form.card" placeholder="请输入身份证号" maxlength="18" show-word-limit/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" maxlength="11" show-word-limit/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.zhwy_hirer_state"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
          <!--          <el-select v-model="form.state" placeholder="请选择账号状态">-->
          <!--            <el-option-->
          <!--              v-for="dict in dict.type.zhwy_hirer_state"-->
          <!--              :key="dict.value"-->
          <!--              :label="dict.label"-->
          <!--              :value="parseInt(dict.value)"-->
          <!--            ></el-option>-->
          <!--          </el-select>-->
        </el-form-item>
        <!--        <el-form-item label="租赁的商铺个数" prop="rentCount">-->
        <!--          <el-input v-model="form.rentCount" placeholder="请输入租赁的商铺个数" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="拥有的商铺个数" prop="haveCount">-->
        <!--          <el-input v-model="form.haveCount" placeholder="请输入拥有的商铺个数" />-->
        <!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



  </div>

   <!--   租户信息，明细-->
  <div v-else>
    <transition name="el-zoom-in-center">
      <div class="Jpreview-main flow-form-main nohead flex-container">

        <el-tabs class="Jel_tabs" style="padding: 0 10px">
          <el-tab-pane label="业主信息">

            <div class="btns" >
              <el-button @click="goBack" type="primary">返 回</el-button>
            </div>

            <div class="app-container">
              <el-row :gutter="20">
                <el-col :span="6" :xs="24">
                  <el-card class="box-card">
                    <div slot="header" class="clearfix">
                      <span>业主信息</span>
                    </div>
                    <div>

                      <ul class="list-group list-group-striped">
                        <li class="list-group-item">
                          <svg-icon icon-class="user" />
                          业主姓名
                          <div class="pull-right">{{ user.name }}</div>
                        </li>
                        <li class="list-group-item">
                          <svg-icon icon-class="phone" />
                          手机号码
                          <div class="pull-right">{{ user.phone }}</div>
                        </li>
                        <li class="list-group-item">
                          <i class="icon-item el-icon-postcard" style="font-size: 20px; margin-right: 10px" />
                          身份证号
                          <div class="pull-right">{{ user.card }}</div>
                        </li>
                        <li class="list-group-item">
                          <svg-icon icon-class="email" />
                          用户邮箱
                          <div class="pull-right">{{ user.email }}</div>
                        </li>
                        <li class="list-group-item">
                          <svg-icon icon-class="date" />
                          创建日期
                          <div class="pull-right">{{ parseTime(user.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
                        </li>
                      </ul>
                    </div>
                  </el-card>
                </el-col>

                <el-col :span="18" :xs="24">
                  <el-card>
                    <div slot="header" class="clearfix">
                      <span>基本资料</span>
                    </div>

                    <el-tabs v-model="activeTab" >
                      <el-tab-pane label="商铺信息" name="houseInfo" >
                        <el-table v-loading="loading" :data="currentContracts">

                          <el-table-column prop="shopCode" label="商铺编号" align="left">
                            <template slot-scope="scope">{{scope.row.shopCode }}</template>
                          </el-table-column>

                          <el-table-column label="类型" prop="type" align="left">
                            <template slot-scope="scope">
                              <dict-tag :options="dict.type.zhwy_rental_contract_type" :value="scope.row.type"/>
                            </template>
                          </el-table-column>

                          <el-table-column label="开始时间" prop="startTime"  aligin="left">
                            <template slot-scope="scope">
                              {{ scope.row.startTime }}
                            </template>
                          </el-table-column>
                          <el-table-column label="结束时间" prop="endTime" aligin="left">
                            <template slot-scope="scope">
                              {{scope.row.endTime }}
                            </template>
                          </el-table-column>
                        </el-table>
                      </el-tab-pane>
                      <el-tab-pane label="历史商铺信息" name="houseHistory">
                        <el-table v-loading="loading" :data="historyContracts">
                          <el-table-column prop="block" label="商铺编号" align="left">
                            <template slot-scope="scope">{{scope.row.shopCode }}</template>
                          </el-table-column>
                          <el-table-column label="类型" prop="state" algin="left">
                            <template slot-scope="scope">
                              <dict-tag :options="dict.type.zhwy_rental_contract_type" :value="scope.row.type"/>
                            </template>
                          </el-table-column>
                          <el-table-column label="开始时间" prop="beginDate" algin="left">
                            <template slot-scope="scope">
                              {{ scope.row.startTime }}
                            </template>
                          </el-table-column>
                          <el-table-column label="结束时间" prop="deleteTime" algin="left">
                            <template slot-scope="scope">
                              {{scope.row.endTime }}
                            </template>
                          </el-table-column>
                        </el-table>
                      </el-tab-pane>
                      <el-tab-pane label="待缴费信息" name="fee">
                        <el-table
                          v-loading="loading"
                          :data="unpaiedBills"
                          ref="showTable"
                        >
                          <el-table-column prop="chargenames" label="收费项名称" align="left"></el-table-column>
                          <el-table-column prop="startTime" label="起收日期" align="left"/>
                          <el-table-column prop="endTime" label="到期日期" align="left"/>
                          <el-table-column prop="deadlineTime" label="缴费限期" align="left"/>
                          <el-table-column  label="单价" prop="univalence"align="left"/>
                          <el-table-column label="数量" prop="number" algin="left"/>
                          <el-table-column label="金额" prop="money" algin="left"/>
                        </el-table>
                      </el-tab-pane>
                      <el-tab-pane label="历史缴费信息" name="feeHistory">
                        <el-table
                          v-loading="loading"
                          :data="paiedBills"
                          ref="showTable"
                        >
                          <el-table-column prop="chargenames" label="收费项名称" align="left"></el-table-column>
                          <el-table-column prop="startTime" label="起收日期" align="left"/>
                          <el-table-column prop="endTime" label="到期日期" align="left"/>
                          <el-table-column prop="fundsReceived" label="实收金额" align="left"/>
                          <el-table-column label="缴费时间" prop="chargeTime" algin="left"/>
                          <el-table-column prop="totalRefundAmount" label="退款金额" align="left"/>
                        </el-table>
                      </el-tab-pane>
                    </el-tabs>
                  </el-card>
                </el-col>

              </el-row>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </transition>

  </div>



</template>

<script>
import { listHirer, getHirer, addHirer, updateHirer } from "@/api/renter/hirer";
import {listRentalcontract, getRentalcontract} from "@/api/rentalcontract/rentalcontract";
import { listChargedata } from "@/api/chargedata/chargedata";



export default {
  name: "Hirer",
  dicts: ['sys_user_sex', 'zhwy_hirer_state', 'zhwy_rental_contract_type'],

  data() {
    return {
     // hhh:[],
      // rentalcontractInfo:[],
      unpaiedBillstest:[],
      unpaiedBills:[],
      // 历史缴费
      paiedBills:[],
      // 当前商铺
      currentContracts: [],
      currentss:[],
      // 历史商铺
      houseHistory:[],
      historyContracts:[],
      activeTab: 'houseInfo',
      id:'',
      // shopstate:'',
      // 业主信息
      user:{},
      // 是否显示detail
      showDetail:true,
      // 遮罩层
      //
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租户信息表格数据
      hirerList: [],
      rentalcontractList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        card: null,
        phone: null,
        hirerId: "",

      },
      // 当前时间
      currentBeijingTime:null,
      // 代缴费数据
      queryParamsPay: {
        pageNum: 1,
        pageSize: 10,
        chargeUser:null,

      },
      //设置全局客户名
      chargeNames: null,

      rentCounts:'',
      haveCounts:'',
      // 查询租售合同
      queryParamsCon: {
        pageNum: 1,
        pageSize: 10,
        shopsId: "",
        type: null,
        id:'',
        hirerId: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        company: [
          { required: true, message: "公司名不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "业主姓名不能为空", trigger: "blur" }
        ],
        card: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCurrentBeijingTime();
  },
  methods: {
    // 获取当前时间
    getCurrentBeijingTime(){
      const now = new Date();
      // 添加时区偏移量来转换为北京时间
      now.setHours(now.getHours() +8);
      this.currentBeijingTime = now;
    },

    goBack() {
      this.showDetail = true

    },
    // 查询商铺信息
    lis(){
      listRentalcontract(this.queryParams).then(response => {
        this.currentss = response.rows
        for(let it of this.currentss){
          if(new Date(it.endTime) < this.currentBeijingTime){
            this.historyContracts.push(it)
          }
          if(new Date(it.endTime) > this.currentBeijingTime){
            this.currentContracts.push(it)
          }
        }
        console.log(this.currentContracts)
      })
    },
    //历史商铺


    // 待缴费信息
    payBehalf(){
      listChargedata(this.queryParamsPay).then(response => {
      this.unpaiedBillstest = response.rows
        // 时间判断
        for(let it of this.unpaiedBillstest){
          if(new Date(it.endTime) < this.currentBeijingTime){
            this.paiedBills.push(it)
          }
          if(new Date(it.endTime) > this.currentBeijingTime){
            this.unpaiedBills.push(it)
          }
        }
      console.log(this.unpaiedBills)
      })
    },
    //
    /** 打开明细 */
    handleDetail(row) {
      this.id = row.id
      this.queryParams.hirerId=row.id;

      this.chargeNames = row.name
      this.queryParamsPay.chargeUser = row.name
      this.showDetail = false;
      // 根据id查询租户信息
      getHirer(this.id).then(response => {
        this.user = response.data
      })
      this.lis()
      this.payBehalf()

      // getRentalcontract(this.id).then(resp=>{
      //   this.currentContracts = resp.data
      //   console.log(this.currentContracts)
      // })

      // 查询租售合同信息
      // listRentalcontract({id:this.id}).then(resp=>{
      //   this.currentContracts = resp.rows
      //
      //   console.log(this.currentContracts)
      //   console.log(this.currentContracts.shopsId)
        // 查询商铺信息
        // listShops({id:1}).then(resp=>{
        //   this.currentContracts = resp.rows
        //
        // })

      // })


    },

    // sumss(){
    //   this.queryParamsCon.hirerId = this.hirerList.id
    //   for(let item of this.hirerList){
    //     listRentalcontract(this.queryParamsCon).then(response => {
    //       this.rentalcontractList = response.rows;
    //       for(let it of this.rentalcontractList){
    //         if(item.id == it.hirerId){
    //           // item.haveCount++;
    //           //    if(it.type==0){
    //           //     item.rentCount++;
    //           //    }
    //           if(it.type==0){
    //             //租赁的个数
    //             item.rentCount++;
    //           }else{
    //             // 拥有的个数1
    //             item.haveCount++;
    //           }
    //
    //         }
    //       }
    //     });
    //   }
    // },

    /** 查询租户信息列表 */
    getList() {

      this.loading = true;
      listHirer(this.queryParams).then(response => {
        this.hirerList = response.rows;
        this.total = response.total;
        this.loading = false;
        for(let item of this.hirerList){
          listRentalcontract(this.queryParamsCon).then(response => {
            this.rentalcontractli = response.rows
            for(let it of this.rentalcontractli){
              if(item.id == it.hirerId){
                if(it.type == 0){
                  this.rentCounts = item.rentCount++
                }else{
                  this.haveCounts = item.haveCount++
                }
              }
            }
          })
        }
      });
      this.hirerList = [];
      this.rentalcontractli = [];
     // this.sumss()
      // getRentalcontract(this.id).then(response => {
      //   this.shopstate = response.data.type;
      //   this.loading = false;
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        company: null,
        name: null,
        card: null,
        phone: null,
        email: null,
        sex: null,
        state: null,
        rentCount: null,
        haveCount: null,
        remark: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租户信息";
      this.form = {
        state : 0
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHirer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHirer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHirer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除租户信息编号为"' + ids + '"的数据项？').then(function() {
    //     return delHirer(ids);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('renter/hirer/export', {
    //     ...this.queryParams
    //   }, `hirer_${new Date().getTime()}.xlsx`)
    // }


  }
};

</script>

<style scoped>
  .btns{

  }
</style>

